home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / SQL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-28  |  6.3 KB  |  353 lines

  1. #include "global.h"
  2. #ifdef SQL
  3. #include "commands.h"
  4. #include "socket.h"
  5. #include "sql.h"
  6.  
  7.  
  8. #if !defined(_lint)
  9. static char rcsid[] OPTIONAL = "$Id: sql.c,v 1.11 1996/12/29 02:47:22 root Exp root $";
  10. #endif
  11.  
  12. static char *SQL_HOST;
  13. static char *SQL_DATABASE;
  14. static void separatorline (m_result *result);
  15. static int SQL_LIMIT = 1;
  16.  
  17. #define SQLRETRIES 20
  18.  
  19.  
  20. static int dosqlquery (int argc,char *argv[],void *p);
  21. static int dosqluserquery (int argc,char *argv[],void *p);
  22. static int dosqlhost (int argc,char *argv[],void *p);
  23. static int dosqllimit (int argc,char *argv[],void *p);
  24. static int dosqldatabase (int argc,char *argv[],void *p);
  25.  
  26. /* sql subcommand table */
  27. static struct cmds SOUNDtab[] = {
  28.     { "database",        dosqldatabase,        0, 0, NULLCHAR },
  29.     { "host",        dosqlhost,        0, 0, NULLCHAR },
  30.     { "limitusers",        dosqllimit,        0, 0, NULLCHAR },
  31.     { "query",        dosqlquery,        0, 2, "sql query \"query string\"" },
  32.     { "userquery",        dosqluserquery,        0, 2, "sql userquery \"select query string\"" },
  33.     { NULLCHAR,        NULL,            0, 0, NULLCHAR }
  34. };
  35.  
  36.  
  37. int
  38. dosql(argc,argv,p)
  39. int argc;
  40. char *argv[];
  41. void *p;
  42. {
  43.     return subcmd(SOUNDtab,argc,argv,p);
  44. }
  45.  
  46.  
  47. static int
  48. dosqldatabase(argc,argv,p)
  49. int argc;
  50. char *argv[];
  51. void *p OPTIONAL;
  52. {
  53.     if(argc < 2)    {
  54.         if (SQL_DATABASE)
  55.             tprintf ("%s\n", SQL_DATABASE);
  56.     } else {
  57.         if (SQL_DATABASE)
  58.             free (SQL_DATABASE);
  59.         SQL_DATABASE = strdup (argv[1]);
  60.     }
  61.     return 0;
  62. }
  63.  
  64.  
  65. static int
  66. dosqllimit(argc,argv,p)
  67. int argc;
  68. char *argv[];
  69. void *p OPTIONAL;
  70. {
  71.     return setbool(&SQL_LIMIT,"Limit non-SYSOPs to only 'select' queries",argc,argv);
  72. }
  73.  
  74.  
  75. static int
  76. dosqlhost(argc,argv,p)
  77. int argc;
  78. char *argv[];
  79. void *p OPTIONAL;
  80. {
  81.     if(argc < 2)    {
  82.         if (SQL_HOST)
  83.             tprintf ("%s\n", SQL_HOST);
  84.     } else {
  85.         if (SQL_HOST)
  86.             free (SQL_HOST);
  87.         SQL_HOST = strdup (argv[1]);
  88.     }
  89.     return 0;
  90. }
  91.  
  92.  
  93. static int
  94. dosqlquery(argc,argv,p)
  95. int argc OPTIONAL;
  96. char *argv[];
  97. void *p OPTIONAL;
  98. {
  99.     (void) sql_query (argv[1]);
  100.     return 0;
  101. }
  102.  
  103.  
  104.  
  105. static int
  106. dosqluserquery(argc,argv,p)
  107. int argc OPTIONAL;
  108. char *argv[];
  109. void *p OPTIONAL;
  110. {
  111.     if (SQL_LIMIT && strnicmp (argv[1], "select ", 7))
  112.         tprintf ("Sorry, only SQL 'select' commands are availble to non-SYSOPs\n");
  113.     else
  114.         (void) sql_query (argv[1]);
  115.     return 0;
  116. }
  117.  
  118.  
  119.  
  120. void
  121. sql_init ()
  122. {
  123.     SQL_DATABASE = strdup ("tnos");
  124. }
  125.  
  126.  
  127. /* This opens the sql server, and sets the database to dbname */
  128. int
  129. sql_open (dbname)
  130. char *dbname;
  131. {
  132. int sock;
  133.  
  134.     if ((sock = msqlConnect (SQL_HOST)) != -1)
  135.         if (msqlSelectDB (sock, dbname) != -1)
  136.             return sock;
  137.     return -1;
  138. }
  139.  
  140.  
  141. void
  142. sql_error ()
  143. {
  144.     tprintf ("SQL ERROR: %s\n", msqlErrMsg);
  145. }
  146.  
  147.  
  148. void
  149. sql_close (sock)
  150. int sock;
  151. {
  152.     msqlClose (sock);
  153. }
  154.  
  155.  
  156. void
  157. sql_endquery (result)
  158. m_result *result;
  159. {
  160.     msqlFreeResult(result);
  161. }
  162.  
  163.  
  164. m_result *
  165. sql_run (sock, query)
  166. int sock;
  167. char *query;
  168. {
  169. m_result *result;
  170. int count;
  171. int retries;
  172.  
  173.     for (retries = 0; retries < SQLRETRIES; retries++) {
  174.         if (msqlQuery (sock, query) != -1)
  175.             break;
  176.         (void) kpause (500);
  177.     }
  178.     if (retries == SQLRETRIES)    {
  179.         sql_error ();
  180.         return NULLSQLRESULT;
  181.     }
  182.     tprintf ("\nQuery OK.\n\n");
  183.     result = msqlStoreResult();
  184.     if (result)    {
  185.         count = msqlNumRows(result);
  186.         tprintf("%d row%s matched.\n\n",count, (count != 1) ? "s" : "");
  187.     }
  188.     return (result);
  189. }
  190.  
  191.  
  192. static void
  193. separatorline (result)
  194. m_result *result;
  195. {
  196. m_field    *curField;
  197. int length = 0;
  198. int k;
  199.  
  200.     /* Print a pretty header .... */
  201.     tputs (" +");
  202.  
  203.     /* first calculate size */
  204.     while((curField = msqlFetchField(result)) != ((m_field *)0))    {
  205.         switch(curField->type)    {
  206.                 case REAL_TYPE:
  207.                 length = (int) strlen(curField->name);
  208.                 if (length < 12)
  209.                     length = 12;
  210.                 break;
  211.  
  212.                 case INT_TYPE:
  213.                 length = (int) strlen(curField->name);
  214.                 if (length < 8)
  215.                     length = 8;
  216.                 break;
  217.  
  218.                 case CHAR_TYPE:
  219.                 length = (int)strlen(curField->name);
  220.                 length = max(length, curField->length);
  221.                 break;
  222.  
  223.             default:
  224.                 break;
  225.         }
  226.         for (k = 0; k < length; k++)
  227.             tputc ('-');
  228.         tputs ("-+");
  229.     }
  230.     tputs ("\n");
  231.     msqlFieldSeek(result,0);
  232. }
  233.  
  234. void
  235. sql_printheader (result)
  236. m_result *result;
  237. {
  238. m_field    *curField;
  239. int length = 0;
  240.  
  241.     separatorline (result);
  242.     tprintf(" |");
  243.     while((curField = msqlFetchField(result)) != ((m_field *)0))    {
  244.         switch(curField->type)    {
  245.                 case INT_TYPE:
  246.                 length = (int) strlen(curField->name);
  247.                 if (length < 8)
  248.                     length = 8;
  249.                 break;
  250.  
  251.                 case REAL_TYPE:
  252.                 length = (int) strlen(curField->name);
  253.                 if (length < 12)
  254.                     length = 12;
  255.                 break;
  256.  
  257.                 case CHAR_TYPE:
  258.                 length = (int)strlen(curField->name);
  259.                 length = max(length, curField->length);
  260.                 break;
  261.  
  262.             default:
  263.                 break;
  264.         }
  265.         tprintf(" %-*.*s|", length, length, curField->name);
  266.     }
  267.     tputs("\n");
  268.     msqlFieldSeek(result,0);
  269.     separatorline (result);
  270. }
  271.  
  272.  
  273. void
  274. sql_printresults (result)
  275. m_result *result;
  276. {
  277. m_row cur;
  278. m_field *curField;
  279. int off, length = 0;
  280.  
  281.     /* Print the returned data */
  282.     while ((cur = msqlFetchRow(result)) != ((m_row)0))    {
  283.         off = 0;
  284.         tprintf(" |");
  285.         while(off < msqlNumFields(result))    {
  286.             curField = msqlFetchField(result);
  287.             switch(curField->type)        {
  288.                 case INT_TYPE:
  289.                 length = (int) strlen(curField->name);
  290.                 if (length < 8)
  291.                     length = 8;
  292.                 break;
  293.  
  294.                 case REAL_TYPE:
  295.                 length = (int) strlen(curField->name);
  296.                 if (length < 12)
  297.                     length = 12;
  298.                 break;
  299.  
  300.                 case CHAR_TYPE:
  301.                 length = (int)strlen(curField->name);
  302.                 length = max(length, curField->length);
  303.                 break;
  304.  
  305.                 default:
  306.                     break;
  307.             }
  308.             if (cur[off])
  309.                 tprintf(" %-*.*s|", length, length, cur[off]);
  310.             else
  311.                 tprintf(" %-*.*s|", length, length, "NULL");
  312.             off++;
  313.         }
  314.         tprintf("\n");
  315.         msqlFieldSeek(result,0);
  316.     }
  317.     separatorline (result);
  318.     tputs ("\n");
  319. }
  320.  
  321.  
  322. int
  323. sql_query (query)
  324. char *query;
  325. {
  326. int sock;
  327. m_result *result;
  328. int retries;
  329.  
  330.     for (retries = 0; retries < SQLRETRIES; retries++)    {
  331.         if ((sock = sql_open (SQL_DATABASE)) != -1)
  332.             break;
  333.         (void) kpause (500);
  334.     }
  335.     if (retries == SQLRETRIES)    {
  336.         sql_error ();
  337.         return -1;
  338.     }
  339.     result = sql_run (sock, query);
  340.     if (result != NULLSQLRESULT)    {
  341.         sql_printheader (result);
  342.         sql_printresults (result);
  343.         sql_endquery (result);
  344.     }
  345.     sql_close (sock);
  346.     return 0;
  347. }
  348.  
  349.  
  350.  
  351. #endif /* SQL */
  352.  
  353.